package com.imc.api.templmess.service;

import com.imc.api.templmess.pojo.TempMess;
import com.imc.api.templmess.service.http.ITempMessService;
import com.imc.api.templmess.utils.TemplateData;
import com.imc.api.templmess.utils.WX_TemplateMsgUtil;
import com.imc.api.templmess.utils.WeiXinEnum;
import com.imc.core.annotation.Logs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

import static com.imc.api.templmess.utils.WX_TemplateMsgUtil.packJsonmsg;

/*
 * 类描述：模板消息服务类
 * 开发者：YT
 * 创建日期：2018/10/28 22:05
 */
@Transactional
@Slf4j
@Service
public class TempMessServiceImpl implements ITempMessService {

    /*
     * YT  2018/10/28 22:06
     * 方法描述：发送模板消息
     */
    @Logs
    @Override
    public String send(TempMess tempMess, String appid, String appsecret) {
        String templateId = null;
        if ("1".equals(tempMess.getTemplType())) {
            templateId = WeiXinEnum.WX_TEMPLATE_MSG_NUMBER.ORDER_PAYED_SUCCESS.getTemplateId();
        } else if ("2".equals(tempMess.getTemplType())) {
            templateId = WeiXinEnum.WX_TEMPLATE_MSG_NUMBER.TEMPL_MESS_SEND_SUCCESS.getTemplateId();
        } else {
            templateId = WeiXinEnum.WX_TEMPLATE_MSG_NUMBER.TEMPL_MESS_SEND_SUCCESS.getTemplateId();
        }
        senMsg(templateId, appid, appsecret, tempMess);

        return "0";
    }

    /*
     * YT  2018/10/28 22:07
     * 方法描述：发送模板消息
     */
    @Logs
    static void senMsg(String templateId, String appid, String appsecret, TempMess tempMess) {
        //新增用户成功 - 推送微信消息
        //姓名 {{keyword1.DATA}}
        //电话{{keyword2.DATA}}
        //地址 {{keyword3.DATA}}
        //预约项目 {{keyword4.DATA}}
        //体验时间 {{keyword5.DATA}}
        //预约码 {{keyword6.DATA}}
        //课程金额 {{keyword7.DATA}}
        //用户是否订阅该公众号标识 (0代表此用户没有关注该公众号 1表示关注了该公众号)
        //Integer state= WX_UserUtil.subscribeState(openId,appid,appsecret);
        // 绑定了微信并且关注了服务号的用户 , 注册成功-推送注册短信
        //if(state==1){
        String openId = tempMess.getOpenId();//小程序"odJAr5JtvH9HoLe6aKfoJi4c7rWc"; //我的公众号oFv1L0_ImhaW86lGq9B4B4_q6ZUk
        String formId = tempMess.getFormId();
        String page = tempMess.getPage();
        log.info("openId" + openId + " formId:" + formId);
        Map <String, TemplateData> param = new HashMap <>();
        param.put("keyword1", new TemplateData(tempMess.getName(), "#696969"));
        param.put("keyword2", new TemplateData(tempMess.getTel(), "#696969"));
        param.put("keyword3", new TemplateData(tempMess.getAddr(), "#696969"));
        param.put("keyword4", new TemplateData(tempMess.getLessonName(), "#696969"));
        param.put("keyword5", new TemplateData(tempMess.getReservationDate(), "#696969"));
        param.put("keyword6", new TemplateData(tempMess.getReservationCode(), "#696969"));
        param.put("keyword7", new TemplateData(tempMess.getLessonAmounts(), "#696969"));
        //注册的微信-模板Id
        String regTempId = templateId;
        log.info("regTempId" + regTempId);
        //调用发送微信消息给用户的接口
        WX_TemplateMsgUtil.getInstance().sendWechatMsgToUser(page, formId, openId, regTempId, "", "#000000", packJsonmsg(param), appid, appsecret);
        // }
    }
}
